<template>
  <div class="unitWrapper">
    <div class="unitTop">
      <div class="unitTitleContainer">
        <div class="unitFlag"></div>
        <div class="unitTitle">职业发展分析规划</div>
      </div>
      <div class="saveBtns" v-if="!isSuperToken && false">
        <div class="contentItemEditAndAIBtn AIBtn" @click="aiCreate()">
          <img src="@/assets/detail/AI_white.png" />
          {{ aiCreateStatus ? "AI正在生成中 . . ." : "AI生成" }}
        </div>
        <div class="contentItemEditAndAIBtn" @click="handleEditClick">
          <img src="@/assets/detail/edit_white.png" />
          {{ currentEdit ? "退出编辑" : "编辑" }}
        </div>
        <div
          class="contentItemEditAndAIBtn saveEditBtn"
          @click="save()"
          v-if="isAdmin == true && currentEdit && !editAll"
        >
          <!-- <img src="@/assets/detail/save.png" /> -->
          保存
        </div>
      </div>
    </div>
    <div
      :class="{
        unitContent: true,
      }"
    > 
    <div class="swotType">
      <div class="swotType1">职业分析</div>
      <div class="swotType2">职业规划</div>
    </div>
      <div
        class="swot-content"
        v-for="(item, index) in swotContentList"
        :key="index"
      >
        <div class="first-block">{{ item.firstContent }}</div>
        <div class="second-block">
          <div class="top-title">
            <div
              class="title-item"
              v-for="(_itemSecond, _indexSecond) in item.secondTitleList"
              :key="_indexSecond"
            >
              {{ _itemSecond }}
            </div>
          </div>
          <div class="desc" :ref="`desc${index}`">
            <div
              class="content"
              :ref="`content${index}`"
              v-html="item.htmlContent"
            ></div>
            <el-popover placement="top" width="200" trigger="click">
              <div v-html="item.htmlContent"></div>
              <span slot="reference" class="expand-btn"
                ><span>...</span>展开</span
              >
            </el-popover>
          </div>
        </div>
        <div class="third-block">
          <!-- <div class="desc-top">{{ item.thirdContentTitle }}</div>
          <div class="desc-bottom">{{ item.thirdContent }}</div> -->
          <div class="desc-top">{{ item.thirdContent }}</div>
        </div>
        <div class="fourth-block">
          <div class="top-title">
            <div
              class="title-item"
              v-for="(_itemFourth, _indexFourth) in item.fourthTitleList"
              :key="_indexFourth"
            >
              {{ _itemFourth }}
            </div>
          </div>
          <div class="desc" :ref="`desc${index}Fourth`">
            <div
              class="content"
              :ref="`content${index}Fourth`"
              v-html="item.htmlContentFourth"
            ></div>
            <el-popover
              v-if="item.showExpandFourth"
              placement="top"
              width="200"
              trigger="click"
            >
              <div v-html="item.htmlContentFourth"></div>
              <span slot="reference" class="expand-btn"
                ><span>...</span>展开</span
              >
            </el-popover>
          </div>
        </div>
      </div>
      <div class="source-box">
        <span class="source-title">政策信息来源：</span>
        <div
          class="source-data-item"
          v-for="(item, index) in sourceData"
          :key="index"
          
        >
         <span @click="showSourceItem(item)">
          <span v-if="!item.fileName.startsWith('《')">《</span
          >{{ item.fileName
          }}<span v-if="!item.fileName.startsWith('《')">》</span>
         </span>
        </div>
      </div>
    </div>
    <officePreviewDialog
      v-if="officePreviewVisible"
      :dialogVisible.sync="officePreviewVisible"
      :fileName="fileName"
      :fileSrc="fileSrc"
    >
    </officePreviewDialog>
  </div>
</template>

<script>
import { getSwotFile } from "@/api/job-develop-path.js";
import officePreviewDialog from "@/components/OfficePreviewDialog";
export default {
  name: "swot",
  components: {
    officePreviewDialog,
  },
  props: {
    isSuperToken: {
      type: Boolean,
      default: false,
    },
    swotData: {
      type: Object,
      default: {},
    },
  },
  mounted() {
    console.log("swotData", this.swotData);
    this.swotData.data.forEach((item, index) => {
      switch (item.layerKey) {
        case "S":
          // this.swotContentList[0].firstContent = item.description;
          this.swotContentList[0].firstContent = '职业优势';
          this.swotContentList[0].secondTitleList = JSON.parse(
            item.layerValue
          ).keywords;
          this.swotContentList[0].htmlContent = JSON.parse(
            item.layerValue
          ).desc;
          break;
        case "SO":
          // this.swotContentList[0].thirdContentTitle = item.description;
          this.swotContentList[0].thirdContentTitle = item.description;
          this.swotContentList[0].fourthTitleList = JSON.parse(
            item.layerValue
          ).keywords;
          this.swotContentList[0].htmlContentFourth = JSON.parse(
            item.layerValue
          ).desc;
          break;
        case "W":
          this.swotContentList[1].firstContent = item.description;
          this.swotContentList[1].firstContent = '职业劣势';
          this.swotContentList[1].secondTitleList = JSON.parse(
            item.layerValue
          ).keywords;
          this.swotContentList[1].htmlContent = JSON.parse(
            item.layerValue
          ).desc;
          break;
        case "WO":
          this.swotContentList[1].thirdContentTitle = item.description;
          this.swotContentList[1].fourthTitleList = JSON.parse(
            item.layerValue
          ).keywords;
          this.swotContentList[1].htmlContentFourth = JSON.parse(
            item.layerValue
          ).desc;
          break;
        case "O":
          // this.swotContentList[2].firstContent = item.description;
          this.swotContentList[2].firstContent = '前景机会';
          this.swotContentList[2].secondTitleList = JSON.parse(
            item.layerValue
          ).keywords;
          this.swotContentList[2].htmlContent = JSON.parse(
            item.layerValue
          ).desc;
          break;
        case "ST":
          this.swotContentList[2].thirdContentTitle = item.description;
          this.swotContentList[2].fourthTitleList = JSON.parse(
            item.layerValue
          ).keywords;
          this.swotContentList[2].htmlContentFourth = JSON.parse(
            item.layerValue
          ).desc;
          break;
        case "T":
          // this.swotContentList[3].firstContent = item.description;
          this.swotContentList[3].firstContent ='潜在威胁';
          this.swotContentList[3].secondTitleList = JSON.parse(
            item.layerValue
          ).keywords;
          this.swotContentList[3].htmlContent = JSON.parse(
            item.layerValue
          ).desc;
          break;
        case "WT":
          this.swotContentList[3].thirdContentTitle = item.description;
          this.swotContentList[3].fourthTitleList = JSON.parse(
            item.layerValue
          ).keywords;
          this.swotContentList[3].htmlContentFourth = JSON.parse(
            item.layerValue
          ).desc;
          break;
        default:
          break;
      }
    });
    // 为每个 swotContentList 项检查溢出
    this.swotContentList.forEach((_, index) => {
      this.checkOverflow(index);
    });
    this.getSwotFileFn();
  },
  data() {
    return {
      swotContentList: [
        {
          refDesc: "desc0",
          refContent: "content0",
          htmlContent:
            "1.技术<br />熟练度：通常对HT开发工程师通常对HT开发工程师通常对HT开发工程师通常对HT开发工程师通常对HT开发工程师通常对HT开发工程师通常对HT开发工程师通常对HT开发工程师通常对HT开发工程师通常对HT开发工程师通常对HT开发工程师通常对HT开发工程师通常对HT开发工程师通常对HT开发工程师",
          showExpandSecond: true,
          htmlContentFourth: "3.技术<br />熟练度：开发师通常对HT",
          showExpandFourth: true,
          secondTitleList: ["技术熟练度", "项目经验"],
          fourthTitleList: ["技术熟练度", "项目经验"],
          firstContent: "S 优势",
          thirdContentTitle: "SO 策略",
          thirdContent: "利用优势抓住机会",
        },
        {
          refDesc: "desc1",
          refContent: "content1",
          htmlContent:
            "2.技术<br />工<br />常<br />常<br />对HT1.端开发程师通常对HT",
          showExpandSecond: false,
          htmlContentFourth:
            "3.技术<br />熟练度：开发工程师通发工师通发工师通发工师通发工师通发工程师通常发工程师通常常对HT1.技术熟练发工程发工程度：前发工程发工程发工程发工程端开发工程发工程发工程发工程师通常对HT1.技术熟练度：前端开发工程师通常对HT结束了结束了aaaaaaa",
          showExpandFourth: false,
          secondTitleList: ["技术熟练度", "项目经验"],
          fourthTitleList: ["技术熟练度", "项目经验"],
          firstContent: "S 优势",
          thirdContentTitle: "SO 策略",
          thirdContent: "弥补劣势抓住机会",
        },
        {
          refDesc: "desc2",
          refContent: "content2",
          htmlContent:
            "3.技术<br />熟练度：开发工程师通发工师通发工师通发工师通发工师通发工程师通常发工程师通常常对HT1.技术熟练发工程发工程度：前发工程发工程发工程发工程端开发工程发工程发工程发工程师通常对HT1.技术熟练度：前端开发工程师通常对HT",
          showExpandSecond: false,
          htmlContentFourth:
            "3.技术<br />熟练度：开发工程师通发工师通发工师通发工师通发工师通发工程师通常发工程师通常常对HT1.技术熟练发工程发工程度：前发工程发工程发工程发工程端开发工程发工程发工程发工程师通常对HT1.技术熟练度：前端开发工程师通常对HT",
          showExpandFourth: false,
          secondTitleList: ["技术熟练度", "项目经验"],
          fourthTitleList: ["技术熟练度", "项目经验"],
          firstContent: "S 优势",
          thirdContentTitle: "SO 策略",
          thirdContent: "利用优势避免威胁",
        },
        {
          refDesc: "desc3",
          refContent: "content3",
          htmlContent:
            "4.技术<br />熟练度：开发工程师通发工师通发工师通发工师通发工师通发工程师通常发工程师通常常对HT1.技术熟练发工程发工程度：前发工程发工程发工程发工程端开发工程发工程发工程发工程师通常对HT1.技术熟练度：前端开发工程师通常对HT",
          showExpandSecond: false,
          htmlContentFourth:
            "3.技术<br />熟练度：开发工程师通发工师通发工师通发工师通发工师通发工程师通常发工程师通常常对HT1.技术熟练发工程发工程度：前发工程发工程发工程发工程端开发工程发工程发工程发工程师通常对HT1.技术熟练度：前端开发工程师通常对HT",
          showExpandFourth: false,
          secondTitleList: ["技术熟练度", "项目经验"],
          fourthTitleList: ["技术熟练度", "项目经验"],
          firstContent: "S 优势",
          thirdContentTitle: "SO 策略",
          thirdContent: "减少劣势避免威胁",
        },
      ],
      isExpanded: false,
      sourceData: [],
      currentSourceItem: {},
      officePreviewVisible: false,
      fileName: "",
      fileSrc: "",
    };
  },
  methods: {
    checkOverflow(index) {
      this.$nextTick(() => {
        // 这部分是secondBlock的逻辑
        const content = this.$refs[`content${index}`];
        const container = this.$refs[`desc${index}`];

        if (!content?.[0] || !container?.[0]) {
          return;
        }

        const contentHeight = content[0].scrollHeight;
        const containerHeight = container[0].clientHeight;
        if (contentHeight > containerHeight) {
          this.swotContentList[index].showExpandSecond = true;
        } else {
          this.swotContentList[index].showExpandSecond = false;
        }
        // 这部分是fourthBlock的逻辑
        const contentFourth = this.$refs[`content${index}Fourth`];
        const containerFourth = this.$refs[`desc${index}Fourth`];
        if (!contentFourth?.[0] || !containerFourth?.[0]) {
          return;
        }
        const contentFourthHeight = contentFourth[0].scrollHeight;
        const containerFourthHeight = containerFourth[0].clientHeight;
        if (contentFourthHeight > containerFourthHeight) {
          this.swotContentList[index].showExpandFourth = true;
        } else {
          this.swotContentList[index].showExpandFourth = false;
        }
      });
    },
    async getSwotFileFn() {
      const params = {
        dataId: this.$route.query.id,
        fileType: "90",
      };
      const res = await getSwotFile(params);
      if (res.code == 200) {
        this.sourceData = res.data;
      }
    },
    showSourceItem(item) {
      if (item.fileUrl) {
        this.fileSrc = item.fileUrl;
        this.officePreviewVisible = true;
        this.fileName = item.fileName;
      } else {
        // 打开新的标签页
        window.open(item.webUrl, "_blank");
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.unitWrapper {
  display: flex;
  flex-direction: column;
  width: 100%;
  // height: 100px;
  .unitTop {
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    // background: linear-gradient(260deg, #48a8ff 0%, #1470fa 100%);
    border-radius: 8px 8px 0px 0px;
    opacity: 0.87;
    // padding: 10px 18px;
    .unitTitleContainer {
      display: flex;
      align-items: center;
      .unitFlag {
        width: 5px;
        height: 15px;
        background: #4880ff;
        border-radius: 3px;
        margin-right: 8px;
      }
      .unitTitle {
        height: 25px;
        font-size: 18px;
        font-family: PingFangSC-Medium, PingFang SC;
        font-weight: 600;
        letter-spacing: 0.3px;
        color: #000000;
        line-height: 25px;
      }
    }

    .saveBtns {
      display: flex;
      height: 32px;
      align-items: center;
      justify-content: center;
      padding-right: 10px;
      .contentItemEditAndAIBtn {
        line-height: 32px;
        margin: 0 10px 0 15px;
        font-size: 14px;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: #ffffff;
        display: flex;
        align-items: center;
        cursor: pointer;
        img {
          width: 22px;
          height: 22px;
        }
      }
      .saveEditBtn {
        width: 57px;
        height: 26px;
        color: #3674ff;
        background-color: #fff;
        border-radius: 4px;
        justify-content: center;
      }
    }
  }
  .unitContent {
    // height: 600px;
    background: #ffffff;
    border-radius: 0px 0px 8px 8px;
    // border: 1px solid #D7E4FF;
    padding: 14px 0 14px 0px;
    box-sizing: border-box;
    .swotType{
      display: flex;
      &>div{
        display: flex;
        align-items: center;
        justify-content: center;
        width: 50%;
        height: 48px;
        font-size: 20px;
        color: white;
        font-weight: bold;
      }
      .swotType1{
      border-radius: 8px 0 0 0 ;
        background-color: #47b77c;
      }
      .swotType2{
      border-radius:0 8px  0 0 ;
        background-color: #00a7ef;
      }
    }
  }
  .swot-content {
    display: flex;
    .first-block {
      // width: 125px;
      width: 12.5%;
      // height: 150px;
      background: #f2fff9;
      color: #0f8137;
      position: relative;
      display: flex;
      flex-direction: row;
      align-items: center;
      justify-content: center;
      font-size: 18px;
      font-weight: 500;
    }
    .first-block::after {
      content: "";
      position: absolute;
      right: -5px; /* 调整三角形位置 */
      top: 50%;
      transform: translateY(-50%);
      width: 0;
      height: 0;
      border-left: 6px solid #f2fff9; /* 绿色三角形 */
      border-top: 10px solid transparent;
      border-bottom: 10px solid transparent;
    }
    .second-block {
      display: flex;
      flex-direction: column;
        justify-content: space-between;
      // width: 362px;
      width: 37.5%;
      // height: 150px;
      border-top: 1px solid #d7e4ff;
      border-bottom: 1px solid #d7e4ff;
      border-left: none; /* 左边框 */
      border-right: none; /* 右边框 */
      box-sizing: border-box;
      padding: 17px 22px 19px 16px;
      .top-title {
        display: flex;
        align-items: center;
        flex-wrap: wrap;
        flex: 1;
        .title-item {
          // width: 96px;
          padding:0 10px;
          height: 25px;
          background: #e1f7e8;
          border-radius: 100px;
          display: flex;
          align-items: center;
          justify-content: center;
          font-weight: 400;
          font-size: 14px;
          color: #418959;
          margin-right: 9px;
        margin-bottom: 11px;
        }
      }
      .desc {
        width: 100%;
        height: 92px;
        font-family: PingFangSC, PingFang SC;
        font-weight: 400;
        font-size: 14px;
        color: #555555;
        line-height: 22px;
        overflow: hidden;
        position: relative;

        .content {
          width: 100%;
          word-break: break-all;
          line-height: 24px;
        }

        .expand-btn {
          position: absolute;
          bottom: -3px;
          right: 0;
          background: #fff;
          padding: 0 4px;
          cursor: pointer;
          color: #3674ff;
          span {
            color: #555555;
          }
        }
      }
    }
    .third-block {
      // width: 125px;
      width: 12.5%;
      // height: 150px;
      background: linear-gradient(90deg, #e1f8ff 0%, #e3f7fa 100%);
      color: #0082ba;
      position: relative;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      font-size: 18px;
      font-weight: 500;
      .desc-top{
        width: 70px;
        font-size: 16px;
        line-height: 22px;
      }
      .desc-bottom {
        font-size: 12px;
        margin-top: 8px;
      }
    }
    .third-block::after {
      content: "";
      position: absolute;
      right: -5px; /* 调整三角形位置 */
      top: 50%;
      transform: translateY(-50%);
      width: 0;
      height: 0;
      border-left: 6px solid #e3f7fa; /* 绿色三角形 */
      border-top: 10px solid transparent;
      border-bottom: 10px solid transparent;
    }
    .fourth-block {
        display: flex;
        flex-direction: column;
        justify-content: space-between;
      // width: 362px;
      width: 37.5%;
      // height: 150px;
      border-top: 1px solid #d7e4ff;
      border-bottom: 1px solid #d7e4ff;
      border-left: none; /* 左边框 */
      border-right: 1px solid #d7e4ff; /* 右边框 */
      box-sizing: border-box;
      padding: 17px 22px 19px 16px;
      .top-title {
        display: flex;
        align-items: center;
        flex-wrap: wrap;
        flex: 1;
        .title-item {
          // width: 96px;
          padding:0 10px;
          height: 25px;
          background: #e7f8fd;
          border-radius: 100px;
          display: flex;
          align-items: center;
          justify-content: center;
          font-weight: 400;
          font-size: 14px;
          color: #258098;
          margin-right: 9px;
          margin-bottom: 11px;
        }
      }
      .desc {
        // width: 318px;
        width: 100%;
        height: 92px;
        font-family: PingFangSC, PingFang SC;
        font-weight: 400;
        font-size: 14px;
        color: #555555;
        line-height: 22px;
        overflow: hidden;
        position: relative;
        // margin-top: 11px;
        .content {
          width: 100%;
          word-break: break-all;
          line-height: 24px;
        }

        .expand-btn {
          position: absolute;
          bottom: -4px;
          right: 0;
          background: #fff;
          padding: 0 4px;
          cursor: pointer;
          color: #3674ff;
          span {
            color: #555555;
          }
        }
      }
    }
  }
  .source-box {
    margin-top: 13px;
    .source-title{
      height: 31px;
      font-family: PingFangSC, PingFang SC;
      font-weight: 600;
      font-size: 14px;
      color: #555;
      line-height: 28px;
    }
    .source-data-item {
      height: 31px;
      font-family: PingFangSC, PingFang SC;
      font-weight: 400;
      font-size: 14px;
      color: #888;
      line-height: 28px;
      span{
      cursor: pointer;
      &:hover{
        color: #4880ff;
        text-decoration: underline;
      }
      }
    }
  }

  ::v-deep .el-input__inner {
    height: 32px;
    background: #eeeeee;
    border-radius: 2px;
    font-size: 16px;
    font-family: PingFang-SC-Bold, PingFang-SC;
    font-weight: bold;
    color: #333333;
    line-height: 24px;
    outline: none;
  }
}
</style>
